package com.gxc.graph;

import java.util.ArrayList;
import java.util.List;

/**
 * 螺旋矩阵
 * 给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素
 */
public class SpiralOrder {

    public static void main(String[] args) {
        List<Integer> list = spiralOrder(new int[][]{{3, 2}});
        System.out.println("---------");
    }

    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list  = new ArrayList<>();
        recursion(0 , 0, matrix.length-1, matrix[0].length-1, matrix, list);
        return list;
    }

    public static void recursion(int i, int j ,int m, int n, int[][] matrix, List<Integer> list){
        if (i>m || j>n) return;
        if (i == m) {
            for (int k = j; k <= n; k++) {
                list.add(matrix[i][k]);
            }
            return;
        }
        if (j == n) {
            for (int k = i; k <= m; k++) {
                list.add(matrix[k][j]);
            }
            return;
        }
        for (int k = j; k < n; k++) {
            list.add(matrix[i][k]);
        }
        for (int k = i; k < m; k++) {
            list.add(matrix[k][n]);
        }
        for (int k = n; k > i; k--) {
            list.add(matrix[m][k]);
        }
        for (int k = m; k > j; k--) {
            list.add(matrix[k][j]);
        }
        recursion(i+1, j+1, m-1, n-1, matrix, list);
    }
}
